function [PL] = PropagationModel(d, tagcen, readercen, isTrack, i, j, tipo, frequency, mainparam)

%Basic Parameters
%----------------------------------------------------------%
    RFSpeed         = 300;      d0           = 1;        
    Nwall           = 0;                        
    Nfs             = 1.8;      Nobs         = 3.0;           
    TransPower      = 100;      RecvPower    = 0.0;
    attLoss         = 0.0;      variation    = 0.0;

    Lambda          = (RFSpeed / frequency);    
    PL0             = 20 * log10 ( 4 * pi * d0 / Lambda );
    variation       = mainparam * rand(1,1);
    K = 16;

    
if (frequency == 915)   Nfs  = 2.0; Nobs  = 2.8; Af  = 2.5;
end

if (frequency == 1900)  Nfs  = 2.6; Nobs  = 3.8; Af  = 2.5;
end




%   Tipo 1:     Free Space
%----------------------------------------------------------%

    if (tipo == 1) 
        attLoss     =  PL0 + 10 * Nfs * log10(d) + variation; %ncx2rnd(2,mainparam,1,1);
        PL          = (TransPower * (10^(attLoss / 10)));
    end

%chi2rnd(1)
    
%   Tipo 2:     WAF e Tipo 3:   WAF + Erro
%----------------------------------------------------------%
    if (tipo == 2)  || (tipo == 3) 

        if (isTrack == 1) i = 1; 
        end

        if (j == 1)                                 %   Leitor 1: Ambiente 1

            if (tagcen(i,4) == 1)
                Nwall   = 0; n       = Nfs; 
            end

            if (tagcen(i,4) == 2) || (tagcen(i,4) == 4)
                Nwall   = 1;  n       = Nobs;        
            end


            if (tagcen(i,4) == 3) || (tagcen(i,4) == 5) || (tagcen(i,4) == 7)
                Nwall   = 2;  n       = Nobs;        
            end

            if (tagcen(i,4) == 6) || (tagcen(i,4) == 8) || (tagcen(i,4) == 9)
                Nwall   = 3;  n       = Nobs;        
            end

        %Af = 6;             
        end      


        if (j == 2)                             %   Leitor 2: Ambiente 2

            if (tagcen(i,4) == 3)
                Nwall   = 0;    n       = Nfs; 
            end

            if (tagcen(i,4) == 2) || (tagcen(i,4) == 6)
               Nwall    = 1;    n       = Nobs;        
            end


            if (tagcen(i,4) == 5) || (tagcen(i,4) == 1) || (tagcen(i,4) == 9)
                Nwall   = 2;    n       = Nobs;        
            end

            if (tagcen(i,4) == 4) || (tagcen(i,4) == 7) || (tagcen(i,4) == 8)
                Nwall   = 3;    n       = Nobs;        
            end
        %Af = 6;
        end      

        if (j == 3)                    %   Leitor 3: Ambiente 7

            if (tagcen(i,4) == 7)
                Nwall   = 0;    n       = Nobs;        
            end

            if (tagcen(i,4) == 4) || (tagcen(i,4) == 8)
                Nwall   = 1;    n       = Nobs;        
            end


            if (tagcen(i,4) == 1) || (tagcen(i,4) == 9) || (tagcen(i,4) == 5)
                Nwall   = 2;    n       = Nobs;        
            end

            if (tagcen(i,4) == 2) || (tagcen(i,4) == 3) || (tagcen(i,4) == 6)
                Nwall   = 3;    n       = Nfs; 
            end
        %Af = 1;
        end      


        if (j == 4)                    %   Leitor 4: Ambiente 6

            if (tagcen(i,4) == 9)
                Nwall   = 0;    n       = Nobs;        
            end

            if (tagcen(i,4) == 6) || (tagcen(i,4) == 8)
                Nwall   = 1;    n       = Nobs;        
            end

            if (tagcen(i,4) == 3) || (tagcen(i,4) == 5) || (tagcen(i,4) == 7)
                Nwall   = 2;    n       = Nobs;        
            end

            if (tagcen(i,4) == 1) || (tagcen(i,4) == 2) || (tagcen(i,4) == 4)
                Nwall   = 3;    n       = Nfs; 
            end
        %Af = 1;
        end

    if (tipo == 2) 
        attLoss = PL0 + 10 * n * log10(d) +  variation + Nwall * Af; %(ncx2rnd(2,0,1,1)) variation + rand(1,1); %Nwall * Af raylrnd(K)* mainparam
        PL      = (TransPower * (10^(attLoss / 10)));
    end
            
    if (tipo == 3)
         
        if ( i == 17) || (i ==18)

        attLoss = PL0 + 10 * n * log10(d) + Nwall * Af;% + raylrnd(K);
        PL      = attLoss; %(TransPower * (10^(attLoss / 10)));

        else

        attLoss = PL0 + 10 * n * log10(d) + Nwall * Af; %+ raylrnd(K);
        PL      = attLoss; %(TransPower * (10^(attLoss / 10)));

        end    
    
    end

    end
   
end